package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.educenter.utils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-10-17
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //登录的方法
    @Override
    public String login(UcenterMember member) {
        //获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码非空的判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password))
        {
            throw new GuliException(20001,"登录失败，请输入手机号和密码，不能留空");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper =new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);

        if (mobileMember == null)   //没有这个手机号
        {
            throw new GuliException(20001,"登录失败,手机号不存在");
        }

        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式MD5
        if (!MD5.encrypt(password).equals(mobileMember.getPassword()))
        {
            throw new GuliException(20001,"登录失败,密码错误");
        }

        //判断用户是否禁用
        if (mobileMember.getIsDisabled())
        {
            throw new GuliException(20001,"登录失败");
        }

        //登录成功
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }


    //注册的方法
    @Override
    public int register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode();          //验证码
        String mobile = registerVo.getMobile();      //手机号
        String nickname = registerVo.getNickname();  //昵称
        String password = registerVo.getPassword();  //密码

        //昵称,验证码,手机号和密码非空的判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(code))
        {
            throw new GuliException(20001,"注册失败，请输入内容，不能为空");
        }

        //判断手机验证码是否正确
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new GuliException(20001,"注册失败，验证码不正确或已过期（5分钟有效）");
        }

        //判断手机号是否重复
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        //可以使用selectOne方法，但是只是为了查询表中是否有数据，无需知道表中的数据是什么，所以慎重使用，因此换为selectCount方法
//      UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        Integer count = baseMapper.selectCount(wrapper);

        if (count>0)    //大于0表示表里有数据，已经注册过，注册失败
        {
            throw new GuliException(20001,"手机号已存在");
        }

        //数据添加到数据库中
        UcenterMember member=new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        String encrypt = MD5.encrypt(password);   //使用md5加密密码存进数据库
        member.setPassword(encrypt);
        member.setIsDisabled(false);    //  用户不禁用
        member.setAvatar("https://online-teach-file.oss-cn-beijing.aliyuncs.com/teacher/2019/11/07/91871e25-fd83-4af6-845f-ea8d471d825d.png");  //默认头像
        int insert = baseMapper.insert(member);
        return insert;
    }

    //判断数据库表里面是否存在相同微信信息
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    //查询某一天注册人数
    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
